<%@page import="com.winexpress.product.Wine"%>
<%@page import="java.util.List"%>
<%@page import="com.winexpress.product.Category"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.winexpress.util.SessionAttributes"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>


<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>wineXpress Admin Search Wine</title>

        <script src="/WineXpressClient/resources/js/bootstrap.min.js"></script>
    </head>
    <body>
        <t:adminNavigation/>

        <div class="header">
            <div class="logo_img"><img alt="Site Name" src="/WineXpressClient/resources/images/logo.png"></div>
        </div>

        <form class="navbar-form navbar-right" role="search" style="margin-right: 8%" id="search" action="/WineXpressClient/searchManagement/adminSearchResult" method="post">
            <div class="form-group" style="width:50%'">
                <input type="text" class="form-control" name="key" placeholder="Categroy or wine name">
                <input name="search"  type="hidden" value="search"/>
            </div>
            <button type="submit" class="btn btn-default">Search</button>
        </form>
        <br><br><br><br>
        <%  Boolean isSearchCatgory = (Boolean) request.getAttribute("isSearchCatgory");
            if (isSearchCatgory) { %>
        <%  List<Category> clist = (List) request.getAttribute("categoryList");%>
        <% if (!clist.isEmpty()) { %>
        <table class="table table-striped" style="width:84%; margin-left:8%">
            <thead>
                <tr  class="info">
                    <th>Category</th>
                    <th> </th>
                </tr>
            </thead>
            <% for (Object o : clist) {
                    Category c = (Category) o;%>
            <tr style="height: 10%">
                <td style="width: 18%; vertical-align: middle;"> <a href="/WineXpressClient/searchManagement/adminSearchResult?search=searcyByCategory&key=<%=c.getName()%>"><%=c.getName()%></a> </td>
            </tr>   
            <%}
            } else {%>
            <tr>
            <div class="alert alert-warning"style="width: 80%; margin-left: 10%; margin-right: 10%" >
                Sorry, there are no such categories.<br> 
                Please contact us by enquiries if you need further help.</div>             
        </tr>

        <%}%>
    </tbody > 
</table >
<%} else {%>
<div>
    <% Boolean isDelete = (Boolean) request.getAttribute("isDelete");
        System.out.println(isDelete);%>
    <% Boolean isRelist = (Boolean) request.getAttribute("isRelist");%>
    <% Boolean isAdd = (Boolean) request.getAttribute("isAdd");%>
    <% Boolean isEdit = (Boolean) request.getAttribute("isEdit");%>
    <%if (isDelete != null && isDelete) { %>
    <div class="alert alert-success"style="width: 75%; margin-left: 15%; margin-right: 10%" >The wine is deactivated.</div>
    <% } else if (isDelete != null && !isDelete) { %>
    <div class="alert alert-danger" style="width: 75%; margin-left: 15%; margin-right: 10%">There is an error deactivating the wine.</div>
    <% } else if (isRelist != null && isRelist) { %>
    <div class="alert alert-success" style="width: 75%; margin-left: 15%; margin-right: 10%">The wine is relisted.</div>
    <% } else if (isRelist != null && !isRelist) { %>
    <div class="alert alert-danger" style="width: 75%; margin-left: 15%; margin-right: 10%">There is an error relisting the wine.</div>
    <% } else if (isAdd != null && isAdd) {%>
    <div class="alert alert-success" style="width: 75%; margin-left: 15%; margin-right: 10%">The wine is added successfully.</div>
    <% } else if (isAdd != null && !isAdd) {%>
    <div class="alert alert-danger" style="width: 75%; margin-left: 15%; margin-right: 10%">There is an error adding the wine</div>
    <% } else if (isEdit != null && isEdit) {%>
    <div class="alert alert-success" style="width: 75%; margin-left: 15%; margin-right: 10%">The wine is edited successfully.</div>
    <% } else if (isEdit != null && !isEdit) {%>
    <div class="alert alert-danger" style="width: 75%; margin-left: 15%; margin-right: 10%">There is an error editing the wine</div>
    <% } %>
</div>

<div class="listWine">
    <table class="table table-hover" style='width:75%; margin-left: 15%; margin-right: 10%;'>
        <thead>
            <tr>
                <th width="20">ID</th>
                <th>Wine Name</th>
                <th>Category</th>
                <th>Is Active</th>
                <th style='white-space: nowrap;' width="10"> </th>
                <th style='white-space: nowrap;' width="15"> </th>
                <th style='white-space: nowrap;' width="15"> </th>
            </tr>
        </thead>

        <tbody>
            <%  List<Wine> list = (List) request.getAttribute("winesList");%>
            <% if (list!=null&&!list.isEmpty()) {
                    for (Object o : list) {
                        Wine w = (Wine) o;%>
            <tr>
                <td> <%=w.getId()%> </td>
                <td> <%=w.getName()%> </td>
                <td>  Category </td>
                <td>
                    <% if (w.isIsActive() == true) {
                    %>
                    <input type="checkbox"  name="isActive" value="true" checked disabled>
                    <% } else {
                    %> 
                    <input type="checkbox"  name="isActive" value="true" disabled>
                    <% }%>
                </td>
                <td align="right" style='white-space: nowrap;'> 
                    <form class="form-horizontal" role="form" id="editWine"  name="editWine" action="/WineXpressClient/productManagement/editWine" method="post">                         
                        <% if (w.isIsActive() == true) {
                        %>
                        <button type="submit" class="btn btn-default" >Edit</button>                          
                        <input name="edit" id="edit" type="hidden" value="loadEditWine"/>
                        <input name="editWineID" id="editWineID" type="hidden" value="<%=w.getId()%>"/>
                        <% } else {
                        %> 
                        <button type="submit" class="btn btn-default" disabled >Edit</button>                          
                        <input name="edit" id="edit" type="hidden" value="loadEditWine"/>
                        <input name="editWineID" id="editWineID" type="hidden" value="<%=w.getId()%>"/>
                        <% }%>
                    </form> 
                </td>
                <td align="right" style='white-space: nowrap;'>
                    <form class="form-horizontal" role="form" id="deleteWine"  name="deleteWine" action="/WineXpressClient/productManagement/manageWines" method="post"> 
                        <% if (w.isIsActive() == true) {
                        %>
                        <button type="submit" class="btn btn-default" >Delete</button>                          
                        <input name="delete" id="delete" type="hidden" value="deleteWine"/>
                        <input name="deleteID" id="deleteID" type="hidden" value="<%=w.getId()%>"/>
                        <% } else {
                        %> 
                        <button type="submit" class="btn btn-default" disabled>Delete</button>                          
                        <input name="delete" id="delete" type="hidden" value="deleteWine"/>
                        <input name="deleteID" id="deleteID" type="hidden" value="<%=w.getId()%>"/>
                        <% }%>                       
                    </form> 
                </td>
                <td align="right" style='white-space: nowrap;'>
                    <form class="form-horizontal" role="form" id="relistWine"  name="relistWine" action="/WineXpressClient/productManagement/manageWines" method="post"> 
                        <% if (w.isIsActive() != true) {
                        %>
                        <button type="submit" class="btn btn-default" >Relist</button>                          
                        <input name="relist" id="relist" type="hidden" value="relistWine"/>
                        <input name="relistID" id="relistID" type="hidden" value="<%=w.getId()%>"/>
                        <% } else {
                        %> 
                        <button type="submit" class="btn btn-default" disabled>Relist</button>                          
                        <input name="relist" id="relist" type="hidden" value="relistWine"/>
                        <input name="relistID" id="relistID" type="hidden" value="<%=w.getId()%>"/>
                        <% }%>                       
                    </form> 
                </td>
            </tr>   
            <%}
            } else {%>
            <tr>
        <div class="alert alert-info"style="width: 75%; margin-left: 15%; margin-right: 10%" >No Wine in the database.</div>
        </tr>
        <%}%>

        </tbody>
    </table>
</div>

<%}%>

</body>
</html>

